package com.ruoyi.project.demo.controller;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.config.RuoYiConfig;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.PageDomain;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.framework.web.page.TableSupport;
import com.ruoyi.project.demo.domain.RecordEntity;
import com.ruoyi.project.demo.domain.UserOperateModel;
import com.ruoyi.project.demo.mapper.RecordMapper;
import com.ruoyi.project.demo.service.service.RecordService;
import com.ruoyi.project.system.post.domain.Post;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 记录表控制器
 *
 * @author ruoyi
 */
@Controller
@RequestMapping("/record")
public class RecordController {

    /**
     * 记录表数据接口层
     */
    @Autowired
    private RecordService recordService;

    @Autowired
    private RuoYiConfig ruoYiConfig;

    private String prefix = "record";

    /**
     * 模态窗口
     */
    @GetMapping("/record")
    public String record(ModelMap mmap) {
        mmap.put("version", ruoYiConfig.getVersion());
        return "record/record";
    }

    /**
     * 查询记录信息
     */
    @RequiresPermissions("record:list")
    @Log(title = "查询记录信息", businessType = BusinessType.INSERT)
    @PostMapping("/getRecordList")
    @ResponseBody
    public TableDataInfo getRecordList(@Validated RecordEntity recordEntity) {
        /**
         * 返回表格信息
         */
        TableDataInfo rspData = new TableDataInfo();
        /**
         * 接收前端的当前页和页大小
         */
        PageDomain pageDomain = TableSupport.buildPageRequest();
        //赋值当前页
        recordEntity.setPage((pageDomain.getPageNum() - 1) * pageDomain.getPageSize());
        //赋值页大小
        recordEntity.setLimit(pageDomain.getPageNum() * pageDomain.getPageSize());
        //list数据查询
        List<RecordEntity> getRecordList = recordService.getRecord(recordEntity);
        //默认为10条
        int pageSize = pageDomain.getPageSize();
        //当查询结果不足10条时，替换
        if (pageSize > getRecordList.size())
        {
            pageSize = getRecordList.size();
        }
        //subList() 方法用于截取并返回动态数组中的一部分。 (开始索引，到结束索引)
        rspData.setRows(getRecordList.subList(0, pageSize));
        rspData.setTotal(getRecordList.get(0).getTotal());
        return rspData;
    }


    /**
     * 添加记录信息
     */
    @RequiresPermissions("record:add")
    @Log(title = "添加记录信息", businessType = BusinessType.INSERT)
    @PostMapping("/insertRecord")
    @ResponseBody
    public Integer insertRecord(@Validated RecordEntity recordEntity) {
        return recordService.insertRecord(recordEntity);
    }

    /**
     * "修改记录信息"
     */
    @RequiresPermissions("record:update")
    @Log(title = "修改记录信息", businessType = BusinessType.INSERT)
    @PostMapping("/updateRecord")
    @ResponseBody
    public Integer updateRecord(@Validated RecordEntity recordEntity) {
        return recordService.updateRecord(recordEntity);
    }

    /**
     * "删除记录信息"
     */
    @RequiresPermissions("record:remove")
    @Log(title = "删除记录信息", businessType = BusinessType.INSERT)
    @PostMapping("/removeRecord")
    @ResponseBody
    public Integer removeRecord(@Validated RecordEntity recordEntity) {
        return recordService.removeRecord(recordEntity);
    }
}
